<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class mgame extends MY_Model {

	var $theme_data = array(
		'layout_main' => 'layout',
		'left' => 'include/leftcp',
	);
	public $mytable = "game";
	public $primaryKey = "game_id";
	public $resCode = "GAME";

	function __construct() {
		parent::__construct();
	}

	function field() {
		$fieldData = array();
		$fieldData['user_id'] = 0;
		$fieldData['game_title'] = '';
		$fieldData['game_title_ascii'] = '';
		$fieldData['game_code'] = '';
		$fieldData['game_time_create'] = time();
		$fieldData['game_time_update'] = time();
		$fieldData['game_content'] = '';
		$fieldData['game_status'] = 1;
		$fieldData['game_view'] = 0;
		$fieldData['game_thumb'] = '';
		$fieldData['game_thumb_large'] = '';
		$fieldData['game_thumb_social'] = '';
		$fieldData['game_file'] = '';
		$fieldData['seo_h1'] = '';
		$fieldData['seo_description'] = '';
		$fieldData['seo_keyword'] = '';
		$fieldData['seo_title'] = '';
		$fieldData['game_file_rate_height_width'] = 1;
		$fieldData['game_min_scores'] = 0;
		$fieldData['game_key'] = 0;
		$fieldData['game_type'] = 'game';
		$fieldData['game_time_start'] = 0;
		$fieldData['game_time_end'] = 0;
		$fieldData['use_id_create'] = 0;
		$fieldData['game_info'] = '';
		$fieldData['game_video_link'] = '';
		return $fieldData;
	}
	
	function get_by_cate($cate_id) {
		$this->db->distinct();
		$this->db->select('game.*');
		$this->db
			->from($this->mytable)
			->join('game_category as mc', 'game.game_id = mc.game_id');
		if (is_array($cate_id)){
			$this->db
				->where_in('mc.cat_id',$cate_id);
		}else {
			$this->db
				->where('mc.cat_id = '.$cate_id);
		}
		return $this->db->get()->result();
	}
	
	function update_view($game_id = 0){
		//update view
        $this->db->where("game_id", $game_id);
        $this->db->set('game_view', 'game_view+1', false);
        $this->db->update($this->mytable, array());
	}
	/*
	$permission_code:
		'post_game', 'test_game', 'process_game',
	*/
	function permission($permission_code, $admin_id = 0){
		$flag = 0;
		switch($permission_code){
			case 'post_game': 
				$flag = ($this->permission->check("insert", 'GAME_PROCESS') || $this->permission->check("update", 'GAME_PROCESS'));
			break;
			case 'test_game': 
				$flag = $this->permission->check("select", 'GAME_PROCESS');
			break;
			case 'process_game': 
				$flag = $this->permission->check("full", 'GAME_PROCESS');
			break;
		}
		return $flag;
	}
	
	/*
		--------------------------------------------
		$title contains the title of the page you're sending
		$url is the url of the page
		$debug true print out the debug and show xml call and answer
		--------------------------------------------
		the output is an array with two elements:
		status: ok / ko
		msg: the text response from pingomatic
		--------------------------------------------
	 */
	function pingomatic($title,$url,$debug=false) {
		$content='<?xml version="1.0"?>'.
        '<methodCall>'.
        ' <methodName>weblogUpdates.ping</methodName>'.
        '  <params>'.
        '   <param>'.
        '    <value>'.$title.'</value>'.
        '   </param>'.
        '  <param>'.
        '   <value>'.$url.'</value>'.
        '  </param>'.
        ' </params>'.
        '</methodCall>';
		
		$headers="POST / HTTP/1.0\r\n".
		"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)\r\n".
		"Host: rpc.pingomatic.com\r\n".
		"Content-Type: text/xml\r\n".
		"Content-length: ".strlen($content);
		
		if ($debug) nl2br($headers);
		
		$request=$headers."\r\n\r\n".$content;
		$response = "";
		$fs=fsockopen('rpc.pingomatic.com',80, $errno, $errstr);
		if ($fs) { 
			fwrite ($fs, $request); 
			while (!feof($fs)) $response .= fgets($fs); 
			if ($debug) echo "<xmp>".$response."</xmp>";
			fclose ($fs);
			preg_match_all("/<(name|value|boolean|string)>(.*)<\/(name|value|boolean|string)>/U",$response,$ar, PREG_PATTERN_ORDER);
			for($i=0;$i<count($ar[2]);$i++) $ar[2][$i]= strip_tags($ar[2][$i]);
			return array('status'=> ( $ar[2][1]==1 ? 'ko' : 'ok' ), 'msg'=>$ar[2][3] );
		} else { 
			if ($debug) echo "<xmp>".$errstr." (".$errno.")</xmp>"; 
			return array('status'=>'ko', 'msg'=>$errstr." (".$errno.")");
		} 
	}
	
	function youtube_parse_id($url = '')
	{
		if(strlen($url) == 11) return $url;
		$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
		preg_match($pattern, $url, $matches);
		return (isset($matches[1])) ? $matches[1] : '';
		
	}
}

?>